package com.zw.mes.module.shipments.service;


import com.zw.mes.entity.asse.HalfStore;
import com.zw.mes.entity.shipments.InstShipmentsPlanSheet;
import com.zw.mes.entity.shipments.InstShipmentsProductDetail;
import com.zw.mes.entity.shipments.InstShipmentsPurchasedDetail;
import com.zw.mes.module.prodtaskmanager.entity.VwProject;
import com.zw.mes.module.shipments.mapper.InstShipmentsPlanMapper;
import com.zw.mes.module.shipments.model.LinshiProductDetail;
import com.zw.mes.module.shipments.repository.HalfStoreRepository;
import com.zw.mes.module.shipments.repository.PlanSheetRepository;
import com.zw.mes.module.shipments.repository.ProductDetailRepository;
import com.zw.mes.module.shipments.repository.PurchasedDetailRepository;
import com.zw.mes.module.system.service.LogService;
import com.zw.mes.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: China.wsg
 * @Date: 2020/11/10 15:11
 */
@Service
@SuppressWarnings("all")
public class InstShipmentsPlanService {


    @Autowired
    private LogService logService;
    @Autowired
    private InstShipmentsPlanMapper instShipmentsPlanMapper;
    @Autowired
    private PlanSheetRepository planSheetRepository;
    @Autowired
    private ProductDetailRepository productDetailRepository;
    @Autowired
    private PurchasedDetailRepository purchasedDetailRepository;
    @Autowired
    private HalfStoreRepository halfStoreRepository;


    /**
     * 查询所有未提交的发货计划主单
     *
     * @return
     */
    public List<InstShipmentsPlanSheet> selectShipmentsList() {
        return instShipmentsPlanMapper.selectShipmentsList();
    }

    public List<VwProject> findProjectByCode(String q) {
        return instShipmentsPlanMapper.findProjectByCode(q);
    }

    /**
     * 增加发货计划主单
     *
     * @param instShipmentsPlanSheet
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer saveInstShipmentsPlanSheet(InstShipmentsPlanSheet instShipmentsPlanSheet) {
        instShipmentsPlanSheet.setCreateName(ShiroUtils.getUser().getName());
        instShipmentsPlanSheet.setCreateTime(new Date());
        instShipmentsPlanSheet.setUpdateName(ShiroUtils.getUser().getName());
        instShipmentsPlanSheet.setUpdateTime(new Date());
        instShipmentsPlanSheet.setIsDel(0);
        instShipmentsPlanSheet.setComit(1);
        planSheetRepository.save(instShipmentsPlanSheet);
        logService.saveLog("新增发货计划单主单", "项目编号：" + instShipmentsPlanSheet.getProCode() + "<br>项目名称：" + instShipmentsPlanSheet.getProName());
        return 1;
    }

    /**
     * 根据主表ID查询制作件明细表
     */
    public List<InstShipmentsProductDetail> selectProductDetail(InstShipmentsProductDetail instShipmentsProductDetail) {
        return instShipmentsPlanMapper.selectProductDetail(instShipmentsProductDetail);
    }

    public List<LinshiProductDetail> selectLinshiProductDetail(String proCode) {
        return instShipmentsPlanMapper.selectLinshiProductDetail(proCode);
    }

    /**
     * 保存制作件发货清单，并修改库存数量
     *
     * @param prodPlaDetList
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer saveProductList(List<LinshiProductDetail> prodPlaDetList, String sheetId) {
        //保存选择发货的制作件
        for (int i = 0; i < prodPlaDetList.size(); i++) {
            InstShipmentsProductDetail instShipmentsProductDetail = new InstShipmentsProductDetail();
            instShipmentsProductDetail.setSheetId(Integer.valueOf(sheetId));
            instShipmentsProductDetail.setAsseId(prodPlaDetList.get(i).getId());
            instShipmentsProductDetail.setSource(prodPlaDetList.get(i).getSource());
            //获取库中是否有此制作件，如果有数量相加，没有新增
            List<InstShipmentsProductDetail> instShipmentsProductDetailList = instShipmentsPlanMapper.selectPlanByWid(instShipmentsProductDetail);
            if (instShipmentsProductDetailList.size() > 0) {
                instShipmentsProductDetailList.get(0).setDrawNum(prodPlaDetList.get(i).getNum() + instShipmentsProductDetailList.get(0).getDrawNum());
                instShipmentsProductDetailList.get(0).setUpdateTime(new Date());
                instShipmentsProductDetailList.get(0).setUpdateName(ShiroUtils.getUser().getName());
                productDetailRepository.save(instShipmentsProductDetailList.get(0));
            } else {
                instShipmentsProductDetail.setDrawName(prodPlaDetList.get(i).getDrawName());
                instShipmentsProductDetail.setPart(prodPlaDetList.get(i).getPart());
                instShipmentsProductDetail.setSpecial(prodPlaDetList.get(i).getSpecial());
                instShipmentsProductDetail.setDrawNo(prodPlaDetList.get(i).getDrawNo());
                instShipmentsProductDetail.setDrawNum(prodPlaDetList.get(i).getNum());
                instShipmentsProductDetail.setDrawType(prodPlaDetList.get(i).getDrawType());
                instShipmentsProductDetail.setLocation(prodPlaDetList.get(i).getLocation());
                instShipmentsProductDetail.setCreateName(ShiroUtils.getUser().getName());
                instShipmentsProductDetail.setCreateTime(new Date());
                instShipmentsProductDetail.setUpdateName(ShiroUtils.getUser().getName());
                instShipmentsProductDetail.setUpdateTime(new Date());
                instShipmentsProductDetail.setIsDel(0);
                productDetailRepository.save(instShipmentsProductDetail);
            }
        }
        logService.saveLog("新增发货计划制作件明细", "主单ID：" + sheetId);
        //修改成品库和半成品库库存数量
        for (int i = 0; i < prodPlaDetList.size(); i++) {
            if (prodPlaDetList.get(i).getSource().equals("1")) {
                //半成品库反写数量
                HalfStore halfStore = halfStoreRepository.getOne(prodPlaDetList.get(i).getId());
                halfStore.setNum(halfStore.getNum() - prodPlaDetList.get(i).getNum());
                halfStoreRepository.save(halfStore);
            } else {
                //成品库反写数量
            }
        }

        return 1;
    }

    /**
     * 根据sheetid查询外购件表
     *
     * @param instShipmentsProductDetail
     * @return
     */
    public List<InstShipmentsPurchasedDetail> selectPurchDetail(InstShipmentsProductDetail instShipmentsProductDetail) {
        return instShipmentsPlanMapper.selectPurchDetail(instShipmentsProductDetail);
    }

    /**
     * 保存外购件发货明细
     *
     * @param sheetId
     * @param drawName
     * @param drawNum
     * @param unit
     * @param location
     * @param remark
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer savePurchList(Integer sheetId, String[] drawName, String[] drawNum, String[] unit, String[] location, String[] remark) {
        int del = instShipmentsPlanMapper.delPurchDetailBySheetId(sheetId);
        for (int i = 0; i < drawName.length; i++) {
            InstShipmentsPurchasedDetail instShipmentsPurchasedDetail = new InstShipmentsPurchasedDetail();
            instShipmentsPurchasedDetail.setSheetId(sheetId);
            instShipmentsPurchasedDetail.setDrawName(drawName[i]);
            instShipmentsPurchasedDetail.setDrawNum(drawNum[i]);
            instShipmentsPurchasedDetail.setIsDel(0);
            instShipmentsPurchasedDetail.setLocation(location[i]);
            instShipmentsPurchasedDetail.setRemark(remark[i]);
            instShipmentsPurchasedDetail.setUnit(unit[i]);
            instShipmentsPurchasedDetail.setCreateName(ShiroUtils.getUser().getName());
            instShipmentsPurchasedDetail.setCreateTime(new Date());
            instShipmentsPurchasedDetail.setUpdateName(ShiroUtils.getUser().getName());
            instShipmentsPurchasedDetail.setUpdateTime(new Date());
            purchasedDetailRepository.save(instShipmentsPurchasedDetail);
        }
        logService.saveLog("新增发货计划外购件明细", "主单ID：" + sheetId);
        return 1;
    }

    /**
     * 修改提交状态
     *
     * @param id
     * @return
     */
    public int updateByIdSubmit(Integer id) {
        return instShipmentsPlanMapper.updateByIdSubmit(id);
    }

    /**
     * 删除主单
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateByIdDel(Integer id) {
        InstShipmentsProductDetail instShipmentsProductDetail = new InstShipmentsProductDetail();
        instShipmentsProductDetail.setSheetId(id);
        List<InstShipmentsProductDetail> list = instShipmentsPlanMapper.selectProductDetail(instShipmentsProductDetail);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSource().equals("1")) {
                //半成品库反写数量
                HalfStore halfStore = halfStoreRepository.getOne(list.get(i).getAsseId());
                halfStore.setNum(halfStore.getNum() + list.get(i).getDrawNum());
                halfStoreRepository.save(halfStore);
                list.get(i).setIsDel(1);
                productDetailRepository.save(list.get(i));
            } else {
                //成品库反写数量
            }
        }
        return instShipmentsPlanMapper.updateByIdDel(id);
    }

    /**
     * 删除明细单
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateByIdDelProduct(Integer id) {
        InstShipmentsProductDetail instShipmentsProductDetail = productDetailRepository.getOne(id);
        if (instShipmentsProductDetail.getSource().equals("1")) {
            //半成品库反写数量
            HalfStore halfStore = halfStoreRepository.getOne(instShipmentsProductDetail.getAsseId());
            halfStore.setNum(halfStore.getNum() + instShipmentsProductDetail.getDrawNum());
            halfStoreRepository.save(halfStore);
        } else {
            //成品库反写数量
        }
        return instShipmentsPlanMapper.updateByIdDelProduct(id);
    }
}
